#include <fstream>
#include "TextMap.h"
#include "utils.hpp"

void TextMap::loadMap(std::string const & filename)
{
    std::fstream file;
    int col, row;
    std::string line;

    file.open(filename.c_str(), std::fstream::in);
    if (!file.is_open())
        return;
    file >> _size.y;
    file >> _size.x;

    for (row = 0; !file.eof(); ++row)
    {
        file >> line;
        for (col = 0; col < _size.x; ++col)
            if (line[col] != '.')
                insert_sorted(_objList, GameObject(vector(col * IMap::CASE_SIZE, row * IMap::CASE_SIZE),
                                                   vector(0, 0),
                                                   vector(IMap::CASE_SIZE, IMap::CASE_SIZE)));
    }
    _begin = _objList.begin();
    _end = _objList.end();

    file.close();
}

vector TextMap::getSize() { return _size; }

void TextMap::collision(GameObject & obj, float const elapsedTime) const
{
    GameObjIt it;

    for (it = _begin; it != _end; ++it)
        obj.checkCollision(*it, elapsedTime);
}

void TextMap::draw(sf::RenderWindow & app, sf::Sprite & sprite) const
{
    TextMap::GameObjIt it;

    for (it = _begin; it != _end; ++it)
        (*it).draw(app, sprite);
}
